<?php

namespace App\Admin\Controllers;

use App\Models\Adv;

use Dingo\Blueprint\Action;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Show;
use Illuminate\Support\MessageBag;

class AdvController extends Controller
{
    use ModelForm;

    public $header = '浮窗设置';

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Adv::class, function (Grid $grid) {

            $grid->model()->orderBy('order', 'asc');
            $grid->id('编号')->sortable();
            $grid->order('排序')->editable();
            $grid->title('标题');
            $grid->image('图片')->image(0,50);
            $grid->url('跳转链接');
            $grid->created_at('创建时间');
            $grid->updated_at('更新时间');

            //$grid->disableCreateButton();

            $grid->actions(function (Grid\Displayers\Actions $actions){
                $actions->disableView();
            });

            $grid->filter(function (Grid\Filter $filter){
                $filter->disableIdFilter();
                $filter->like('title', '标题');
                $filter->between('created_at', '创建时间')->datetime();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Adv::class, function (Form $form) {
            $form->display('id', 'ID');
            $form->text('title', '标题')->rules('required');
            $form->text('order', '排序')->default(0)->rules('required');
            $form->image('image', '图片')->rules('required');
            $form->url('url', '跳转链接');
            $form->disableViewCheck();

            $form->tools(function (Form\Tools $tools){
                $tools->disableView();
            });
        });
    }



}
